package com.justplay.mobilelocation.UI;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.justplay.mobilelocation.R;
import com.justplay.mobilelocation.adapter.ContactsAdapter;
import com.justplay.mobilelocation.bean.ContactBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;


public class ContactsActivity extends AppCompatActivity {
    @Bind(R.id.rv_list)
    RecyclerView rv_list;
    private ArrayList<ContactBean> contactBeanArrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        ButterKnife.bind(this);
        try {
            testContacts();
        } catch (Exception e) {
            e.printStackTrace();
        }
        rv_list.setLayoutManager(new LinearLayoutManager(this));

        ContactsAdapter contactsAdapter = new ContactsAdapter(R.layout.contast_item, contactBeanArrayList);
        contactsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ContactBean contactBean = contactBeanArrayList.get(position);
                Intent intent = new Intent();
                intent.putExtra("phone", contactBean.getPhoneNumber());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
        rv_list.setAdapter(contactsAdapter);
    }

    public void testContacts() throws Exception {
        Uri uri = Uri.parse("content://com.android.contacts/contacts");
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);
        while (cursor.moveToNext()) {
            int contactid = cursor.getInt(0);
            ContactBean contactBean = new ContactBean();
            StringBuilder sb = new StringBuilder("contactid=");
            sb.append(contactid);
            uri = Uri.parse("content://com.android.contacts/contacts/" + contactid + "/data");
            Cursor datacursor = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null);
            while (datacursor.moveToNext()) {
                String data = datacursor.getString(datacursor.getColumnIndex("data1"));
                String type = datacursor.getString(datacursor.getColumnIndex("mimetype"));
                if ("vnd.android.cursor.item/name".equals(type)) {//姓名
                    sb.append(",name=" + data);
                    contactBean.setUserName(data);
                } else if ("vnd.android.cursor.item/email_v2".equals(type)) {//email
                    sb.append(",email=" + data);
                } else if ("vnd.android.cursor.item/phone_v2".equals(type)) {//phone
                    sb.append(",phone=" + data);
                    contactBean.setPhoneNumber(data);
                }
            }
            System.out.println(sb.toString());
            contactBeanArrayList.add(contactBean);
        }
    }

}
